package redis.client.nedis.request;

import redis.client.nedis.HostAndPort;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;

/**
 * @author gaodong
 * @version 1.0
 * @date 2020/11/18 13:55
 */
public class RedisRequestQueue {

    /**
     * 单例模式
     */
    static RedisRequestQueue instance = new RedisRequestQueue();

    public static RedisRequestQueue getInstance() {
        return instance;
    }

    ConcurrentMap<HostAndPort, ConcurrentLinkedQueue<RedisRequest>> requestQueue = new ConcurrentHashMap<>();

    public void init(HostAndPort... hps) {
        for (HostAndPort hp : hps) {
            requestQueue.put(hp, new ConcurrentLinkedQueue<>());
        }
    }

    public RedisRequest pop(HostAndPort hp) {
        return requestQueue.get(hp).poll();
    }

    public void off(HostAndPort hp, RedisRequest req) {
        requestQueue.get(hp).offer(req);
    }
}
